package com.step.ems.pages.setting

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.step.ems.common.DataStoreUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SettingViewModel @Inject constructor(
    private val dataStoreUtil: DataStoreUtil
) : ViewModel() {
    var isLogin by mutableStateOf(true)
    var username by mutableStateOf("-")

    init {
        viewModelScope.launch {
            val accessToken = dataStoreUtil.getAccessToken()
            val usernameDs = dataStoreUtil.getUsername()
            isLogin = !accessToken.isNullOrEmpty()
            username = if (isLogin) usernameDs ?: "-" else "-"
        }
    }

    fun logout() {
        viewModelScope.launch {
            dataStoreUtil.logout()
            isLogin = false
            username = "-"
        }
    }
}